<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="指标名称" prop="indicatorsName">
        <el-input
          v-model="queryParams.indicatorsName"
          placeholder="请输入指标名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:indicators:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
          >展开/折叠</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="indicatorsList"
      row-key="indicatorsId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="indicatorsName" label="指标名称"></el-table-column>
      <el-table-column prop="indicatorsType" label="指标大类"></el-table-column>
      <el-table-column prop="indicatorsTab" label="指标小类"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:indicators:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:indicators:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改指标对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="指标名称" prop="indicatorsName">
              <el-input
                v-model="form.indicatorsName"
                placeholder="请输入指标名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标描述" prop="indicatorsDes">
              <el-input
                v-model="form.indicatorsDes"
                placeholder="请输入指标描述"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="指标大类" prop="indicatorsName">
              <el-select
                v-model="form.indicatorsType"
                placeholder="指标大类"
                clearable
                @change="handleChangeType"
              >
                <el-option
                  v-for="dict in dict.type.indicators_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="指标小类" prop="indicatorsName">
              <el-radio-group v-model="form.indicatorsTab">
                <el-radio
                  :label="item.label"
                  v-for="(item, index) in chartTypeS"
                  :key="index"
                >
                <img :src="item.imgUrl" alt="" class="imgTab">
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="指标数据">
              <div class="butoc">
                <el-button icon="el-icon-circle-plus" @click="psuhBtn"
                  >新增</el-button
                >
                <el-button icon="el-icon-remove" @click="deletBtn"
                  >删除</el-button
                >
              </div>
              <footer>
                <div
                  class="base_ones"
                  v-for="(item, index) in form.echarsJson"
                  :key="index"
                >
                  <div class="describe_ones">
                    <div class="describe_set">
                      <div class="set_Left">
                        <el-input
                          placeholder="请输入X轴名称"
                          v-model="item.name"
                          class="field"
                        >
                        </el-input>
                      </div>
                      <div class="set_Rigth">
                        <el-tooltip
                          placement="top-start"
                          effect="dark"
                          style="cursor: pointer"
                        >
                          <div slot="content">
                            用于X轴的名称，饼图各区域名称，雷达图中各维度名称，指标键值对中的key
                          </div>
                          <i class="el-icon-info tip" />
                        </el-tooltip>
                      </div>
                    </div>

                    <div class="describe_set">
                      <div class="set_Left">
                        <el-input
                          placeholder="请输入Y轴数值"
                          v-model="item.value"
                          class="field"
                          style="font-size: 12px"
                        ></el-input>
                      </div>
                      <div class="set_Rigth">
                        <el-tooltip
                          placement="top-start"
                          effect="dark"
                          style="cursor: pointer"
                        >
                          <div slot="content">
                            用于Y轴数值，饼图中的百分比，雷达图各维度值，指标键值对中的value
                          </div>
                          <i class="el-icon-info tip" />
                        </el-tooltip>
                      </div>
                    </div>

                    <div class="describe_set">
                      <div class="set_Left">
                        <el-input
                          placeholder="请输入雷达图各维度的最大值"
                          v-model="item.max"
                          class="field"
                        ></el-input>
                      </div>
                      <div class="set_Rigth">
                        <el-tooltip
                          placement="right-end"
                          effect="dark"
                          style="cursor: pointer"
                        >
                          <div slot="content">用于雷达图各维度的最大值</div>
                          <i class="el-icon-info tip" />
                        </el-tooltip>
                      </div>
                    </div>
                    <div class="describe_set">
                      <div class="set_Left">
                        <el-input
                          placeholder="请输入多组数据数值"
                          v-model="item.group_name"
                          class="field"
                        ></el-input>
                      </div>
                      <div class="set_Rigth">
                        <el-tooltip
                          placement="right-end"
                          effect="dark"
                          style="cursor: pointer"
                        >
                          <div slot="content">
                            用于区分多组数据在同一个图上展示1,2
                          </div>
                          <i class="el-icon-info tip" />
                        </el-tooltip>
                      </div>
                    </div>
                  </div>
                  <div class="vants">
                    <el-checkbox v-model="item.checked"></el-checkbox>
                  </div>
                </div>
              </footer>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指标位置">
              <el-select
                v-model="form.indicatorsSite"
                placeholder="指标位置"
                clearable
              >
                <el-option
                  v-for="dict in orderList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="orderNo">
              <el-input-number
                v-model="form.orderNo"
                :min="1"
                :max="10"
                label="请输入排序"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联事件ID">
              <el-select
                v-model="form.indicatorsSite"
                placeholder="关联事件ID"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属模块ID">
              <el-select
                v-model="form.district"
                placeholder="所属模块ID"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属地区">
              <el-select
                v-model="form.district"
                placeholder="所属地区"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否上图">
              <el-select
                v-model="form.district"
                placeholder="是否上图"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上图数据">
              <el-select
                v-model="form.indicatorsSite"
                placeholder="上图数据"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属平安指数ID">
              <el-select
                v-model="form.indicatorsSite"
                placeholder="所属平安指数ID"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否下钻">
              <el-select
                v-model="form.district"
                placeholder="是否下钻"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="删除标志">
              <el-select
                v-model="form.cIsDelete"
                placeholder="删除标志"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDept,
  getDept,
  delDept,
  addDept,
  updateDept,
  listDeptExcludeChild,
} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import line1 from "@/assets/images/line_1.png"
import line2 from "@/assets/images/line_2.png"
import line3 from "@/assets/images/line_3.png"
import line4 from "@/assets/images/line_4.png"
import curve_1 from "@/assets/images/curve_1.png"
import curve_2 from "@/assets/images/curve_2.png"
import curve_3 from "@/assets/images/curve_3.png"
import curve_4 from "@/assets/images/curve_4.png"
import bar_1 from "@/assets/images/bar_1.png"
import bar_2 from "@/assets/images/bar_2.png"
import bar_3 from "@/assets/images/bar_3.png"
import bar_4 from "@/assets/images/bar_4.png"
import bar_5 from "@/assets/images/bar_5.png"
import pie_1 from "@/assets/images/pie_1.png"
import pie_2 from "@/assets/images/pie_2.png"
import radar_1 from "@/assets/images/radar_1.png"
import radar_2 from "@/assets/images/radar_2.png"
import rank_1 from "@/assets/images/rank_1.png"
import rank_2 from "@/assets/images/rank_2.png"
import dashboard_1 from "@/assets/images/dashboard_1.png"

export default {
  name: "indicators",
  dicts: ["indicators_type", "sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      indicatorsList: [],
      // 指标树选项
      indicatorsOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        indicatorsName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        echarsJson: [
          {
            name: "", // X轴名称
            value: "", // Y轴数值
            max: "", // 雷达图数值
            group_name: "", // 区分多组数值
            checked: false,
          },
        ],
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级指标不能为空", trigger: "blur" },
        ],
        indicatorsName: [
          { required: true, message: "指标名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" },
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
      //指标位置
      orderList: [
        {
          value: 0,
          label: "左侧",
        },
        {
          value: 1,
          label: "右侧",
        },
      ],
      /* echarts类型 */
      graphList: [
        {
          value: "1",
          label: "柱状图",
        },
        {
          value: "2",
          label: "曲线图",
        },
        {
          value: "3",
          label: "折线图",
        },
        {
          value: "4",
          label: "饼图",
        },
        {
          value: "5",
          label: "雷达图",
        },
        {
          value: "6",
          label: "指标键值对",
        },
      ],
      chartType: {
        1: [
          {
            label: "1",
            imgUrl: line1,
          },
          {
            label: "2",
            imgUrl: line2,
          },
          {
            label: "3",
            imgUrl: line3,
          },
          {
            label: "4",
            imgUrl: line4,
          },
        ],
        2: [
          {
            label: "1",
            imgUrl: curve_1,
          },
          {
            label: "2",
            imgUrl: curve_2,
          },
          {
            label: "3",
            imgUrl: curve_3,
          },
          {
            label: "4",
            imgUrl: curve_4,
          },
        ],
        3: [
          {
            label: "1",
            imgUrl: bar_1,
          },
          {
            label: "2",
            imgUrl: bar_2,
          },
          {
            label: "3",
            imgUrl: bar_3,
          },
          {
            label: "4",
            imgUrl: bar_4,
          },
          {
            label: "5",
            imgUrl: bar_5,
          },
        ],
        4: [
          {
            label: "1",
            imgUrl: pie_1,
          },
          {
            label: "2",
            imgUrl: pie_2,
          },
        ],
        5: [],
        7: [
          {
            label: "1",
            imgUrl: radar_1,
          },
          {
            label: "2",
            imgUrl: radar_2,
          },
        ],
        8: [
          {
            label: "1",
            imgUrl: dashboard_1,
          },
        ],
        9: [
          {
            label: "1",
            imgUrl: rank_1,
          },
          {
            label: "2",
            imgUrl: rank_2,
          },
        ],
      },
      chartTypeS:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询指标列表 */
    getList() {
      this.loading = true;
      listDept(this.queryParams).then((response) => {
        this.indicatorsList = this.handleTree(response.data, "indicatorsId");
        this.loading = false;
      });
    },
    /** 转换指标数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.indicatorsId,
        label: node.indicatorsName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   indicatorsId: undefined,
      //   parentId: undefined,
      //   indicatorsName: undefined,
      //   orderNum: undefined,
      //   leader: undefined,
      //   phone: undefined,
      //   email: undefined,
      //   status: "0",
      // };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.indicatorsId;
      }
      this.open = true;
      this.title = "添加指标";
      listDept().then((response) => {
        this.indicatorsOptions = this.handleTree(response.data, "indicatorsId");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getDept(row.indicatorsId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指标";
        listDeptExcludeChild(row.indicatorsId).then((response) => {
          this.indicatorsOptions = this.handleTree(
            response.data,
            "indicatorsId"
          );
          if (this.indicatorsOptions.length == 0) {
            const noResultsOptions = {
              indicatorsId: this.form.parentId,
              indicatorsName: this.form.parentName,
              children: [],
            };
            this.indicatorsOptions.push(noResultsOptions);
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.indicatorsId != undefined) {
            updateDept(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDept(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.indicatorsName + '"的数据项？')
        .then(function () {
          return delDept(row.indicatorsId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 删除按钮
    deletBtn() {
      const list = this.form.echarsJson.filter((item) => {
        return !item.checked;
      });
      this.form.echarsJson = list;
      if (!this.form.echarsJson.length) {
        this.form.echarsJson.push({
          name: "", // X轴名称
          value: "", // Y轴数值
          max: "", // 雷达图数值
          group_name: "", // 区分多组数值
          checked: false,
        });
      }
    },
    // 新增按钮
    psuhBtn() {
      // 新增上一次的图表数据,删除checked属性
      let chartList = JSON.parse(JSON.stringify(this.form.echarsJson));
      chartList.map((item) => delete item.checked);
      let list = [];
      chartList.map((item) => {
        if (
          item.value === "" &&
          item.name === "" &&
          item.max === "" &&
          item.group_name === ""
        ) {
          list.push(item);
        }
      });
      if (list.length === 0) {
        this.form.echarsJson.push({
          name: "", // X轴名称
          value: "", // Y轴数值
          max: "", // 雷达图数值
          group_name: "", // 区分多组数值
          checked: false,
        });
        this.$message({
          message: "新增成功",
          type: "success",
          duration: 800,
        });
      } else {
        this.$message({
          message: "请输入内容后,再试",
          type: "error",
          duration: 800,
        });
      }
      // console.log(this.form,'this.form')
    },
    //
    handleChangeType(val) {
      console.log(val, "val");
      console.log(this.chartType[val]);
      this.chartTypeS = this.chartType[val]
    },
    // //
    // convert(val){
    //   this.chartType[val]
    // }
  },
};
</script>
<style scoped lang="scss">
footer {
  width: 100%;
  // margin-left: 20px;
  display: flex;
  flex-wrap: wrap;
  .base_ones {
    width: 35%;
    /* height: 297px; */
    margin-right: 30px;
    display: flex;
    justify-content: space-between;
    border: 1px solid #ccc;
    padding-right: 33px;
    // margin-bottom: 20px;
    margin-top: 10px;
    margin-right: 60px;
    .content_ones {
      width: 64px;
      height: 21px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 21px;
      color: #191919;
      opacity: 1;
      font-size: 15px;
    }
    .imges {
      width: 88px;
      height: 80px;
      margin-top: 21px;
    }
    .input_ones {
      margin-top: 12px;
    }
    .describe_ones {
      width: 90%;
      height: 241px;
      /* background: #e5e5e5; */
      margin-top: 6px;
      margin-bottom: 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      padding: 20px;
    }
    .describe_set {
      width: 270px;
      height: 36px;
      display: flex;
      justify-content: space-between;
      .set_Left {
        width: 210px;
        height: 36px;
      }
      .set_Rigth {
        width: 40px;
        height: 36px;

        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
::v-deep .el-dialog__body {
  height: 700px;
  overflow-y: scroll;
}
.imgTab {
  width: 80px;
  height: 80px;
  vertical-align: middle;
}
</style>
